package com.freshcity.page.zhitiao
{
	import com.freshcity.core.MainScene;
	import com.freshcity.graphics.ReqDateWin;
	import com.freshcity.page.talk.Talk;
	import com.freshcity.core.OpCodes;
	import com.freshcity.utils.StringUtil;

	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;

	public class RequestDate extends ReqDateWin
	{
		public var group:MainScene;
		private var talk:Talk;

		public function RequestDate(host:Talk)
		{
			this.talk=host;

			this.words.addEventListener(FocusEvent.FOCUS_IN, onPicContentFocus);
			this.words.addEventListener(Event.CHANGE, onPicContentChange);

			this.closeBtn.addEventListener(MouseEvent.CLICK, onCancelAction);
			this.cancelBtn.addEventListener(MouseEvent.CLICK, onCancelAction);
			this.confirmBtn.addEventListener(MouseEvent.CLICK, onSubmitlAction);
		}

		protected function onPicContentChange(event:Event):void
		{
			this.limitWordAmt.text=String(this.words.maxChars);
			this.words.text="";
		}

		protected function onPicContentFocus(event:FocusEvent):void
		{
			var i:int=this.words.maxChars;
			var j:int=StringUtil.getANSILength(this.words.text);
			this.limitWordAmt.text=String(i - j);
			if (j > 0 && i - j >= 0)
			{
				this.confirmBtn.enabled=true;
			}
			else
			{
				this.confirmBtn.enabled=false;
			}
		}

		private function clearListener():void
		{
			this.closeBtn.removeEventListener(MouseEvent.CLICK, onCancelAction);
			this.cancelBtn.removeEventListener(MouseEvent.CLICK, onCancelAction);
			this.confirmBtn.removeEventListener(MouseEvent.CLICK, onSubmitlAction);
		}

		protected function onSubmitlAction(event:MouseEvent):void
		{
			var c:String=this.words.text;
			var d:String=this.dayComBox.selectedItem.label;
			var h:String=this.hourVal.text;
			var m:String=this.minVal.text;

			var s:String="【系统】你正邀请TA于 ".concat(d, ",", h, "点", m, "分", "再次畅谈，我说：", c);
			var woFormat:TextFormat=new TextFormat("宋体", 12, 0x000080, false);
			talk.importOutPut(s, woFormat);
			talk.updateOutput();

			group.commu.send(OpCodes.REQUEST_DATE, {content: c, day: d, hour: h, minute: m}, onCallBack);
		}

		private function onCallBack(data:*):void
		{
			onCancelAction();
		}

		protected function onCancelAction(event:MouseEvent=null):void
		{
			this.clearListener();
			this.parent.removeChild(parent);
		}
	}
}